package ui.activity.poscontrol;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;

import net.ck.hzq.R;

import javax.inject.Inject;

import Utils.StringUtil;
import Utils.ToastUtil;
import base.BaseAct;
import base.BaseBiz;
import butterknife.BindView;
import event.NetErrorEvent;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.poscontrol.beanmodel.ChangebankDetail;
import ui.activity.poscontrol.beanmodel.PosMainBean;
import ui.activity.poscontrol.biz.PosControlMainBiz;
import ui.activity.poscontrol.component.DaggerPosControlMainComponent;
import ui.activity.poscontrol.contract.PosControlMainContract;
import ui.activity.poscontrol.module.PosControlMainModule;
import ui.activity.poscontrol.presenter.PosControlMainPresenter;
import widget.dialog.ChangeBankDialog;
import widget.dialog.MyDialog;


/**
 * 设置返现金额页面
 * Created by chenjian on 18/2/22.
 */

public class ChangeBackAct extends BaseAct implements PosControlMainContract.View {

    @Inject
    PosControlMainPresenter presenter;
    @Inject
    PosControlMainBiz biz;
    @BindView(R.id.oldmoney)
    TextView oldmoney;
    @BindView(R.id.postype)
    TextView postype;
    @BindView(R.id.showmoney)//
            TextView showmoney;
    @BindView(R.id.snnumber)
    TextView snnumber;
    @BindView(R.id.submit_changebank)
    TextView submit_changebank;
    @BindView(R.id.inputmoney)
    EditText inputmoney;
    ChangebankDetail changebankDetail;

    String sn="";
    int maxMoney;
    int minMoney;
    @Override
    protected void setRoot() {
        setContentView(R.layout.activity_changeback);
    }

    @Override
    protected void setup() {
        DaggerPosControlMainComponent.builder().posControlMainModule(new PosControlMainModule(this)).build().inject(this);
        setToolBar("调整返现", "");
        sn = getIntent().getStringExtra("sn");
        biz.queryChangeBankDetail(sn,new BaseBiz.Callback<ChangebankDetail>() {
            @Override
            public void onSuccess(ChangebankDetail posMainBean) {
                oldmoney.setText(  StringUtil.getFormatPriceInt(posMainBean.getBackAmount()+"")+"元");


                postype.setText(posMainBean.getCategory());
                maxMoney = posMainBean.getMax();
                minMoney = posMainBean.getMin();
                showmoney.setText("元（可调整范围"+ StringUtil.getFormatPriceInt(posMainBean.getMin()+"")+"-"+ StringUtil.getFormatPriceInt(posMainBean.getMax()+"")+"元）");

                snnumber.setText(posMainBean.getSn()+"");
            }

            @Override
            public void onFailure(ChangebankDetail getUrlOutput) {

            }

        });
        submit_changebank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(inputmoney.getText().toString().length()<1){
                    ToastUtil.show(ChangeBackAct.this,"金额不得为空");
                    return;
                }
                int inputnum = Integer.parseInt(inputmoney.getText().toString());
                if(inputnum<=minMoney){
                    ToastUtil.show(ChangeBackAct.this,"设置金额必须大于最小返现金额");
                    inputmoney.setText("");
                    return;
                }
                if(inputnum>maxMoney){
                    ToastUtil.show(ChangeBackAct.this,"设置金额不得大于最大返现金额");
                    inputmoney.setText("");
                    return;
                }

                backbean input = new backbean();
                input.setAmount(inputmoney.getText().toString());
                input.setSn(sn);

                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(input));
                biz.submitChangeBack(requestBody,new BaseBiz.Callback<Boolean>() {
                    @Override
                    public void onSuccess(Boolean result) {
                        if(result){
                            ChangeBankDialog changebankDialog = new ChangeBankDialog(ChangeBackAct.this, "", new MyDialog.DialogCallback() {
                                @Override
                                public void OnConfirm() {
                                }

                                @Override
                                public void OnConfirm1() {

                                }

                                @Override
                                public void OnCancel() {

                                }
                            });
                            changebankDialog.show();
                        }
                    }

                    @Override
                    public void onFailure(Boolean result) {

                    }

                });
            }
        });
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {


    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }




    @Override
    public void upDateUI(PosMainBean bean) {
    }

    @Override
    public void showPopWindow() {
    }

    @Override
    public void showFlowRuleDialog(String items) {
    }

    class backbean{
        String amount;
        String sn;

        public String getAmount() {
            return amount;
        }

        public void setAmount(String amount) {
            this.amount = amount;
        }

        public String getSn() {
            return sn;
        }

        public void setSn(String sn) {
            this.sn = sn;
        }
    }
}
